/**
 * RX, RY, RZ = Rotation <br> 
 * TX, TY, TZ = Translation <br> 
 * SX, SY, SZ = Scaling <br>
 * w, h, l = width height length of cube <br>
 * i = number of iterations <br>
 * RF, GF, BF, AF = red green blue alpha of cube fill <br>
 * LW = line weight <br>
 * Mouse left button = rotate view <br>
 * Mouse right button = zoom view <br>
 * Mouse midle button = pan view <br><br>
 * Code writen by Bojan Mitrovic, Faculty of Architecture, Belgrade 2009.
 */
import java.awt.Label;
import java.awt.TextField;
import java.awt.Panel;
import java.awt.BorderLayout;
import java.awt.FlowLayout;

float cx,cy,cz,ax,ay,az;
float ex = 0;
float ey = 0;
// cube dimension
float w = 10;
float h = 10;
float l = 10;
// cube rotation
float rcx = 0;
float rcy = 0;
float rcz = 30;
// cube translate
float tcx = 0;
float tcy = 0;
float tcz = 20;
// cube scale
float scx = 1;
float scy = 1;
float scz = 1;
// cube poss
float pcx = 0;
float pcy = 0;
float pcz = 0;
// cube fill
int ccr = 128;
int ccg = 128;
int ccb = 128;
int cca = 255;
int niter = 3;
// line properties
float lw = 3;
java.awt.TextField trcx, trcy, trcz,
                   ttcx,ttcy,ttcz,
                   tscx,tscy,tscz,
                   tw,th,tl,ti,
                   tccr,tccg,tccb,tcca,
                   tlw;
java.awt.Panel UITop, UI, UI1, UI2;
void setup() 
{  
  size(640, 480, P3D);  // Size should be the first statement
  setLayout(new BorderLayout());
  trcx = new TextField("0",1);
  trcy = new TextField("0",1);
  trcz = new TextField("0",1);
  ttcx = new TextField("0",1);
  ttcy = new TextField("0",1);
  ttcz = new TextField("0",1);
  tscx = new TextField("1",1);
  tscy = new TextField("1",1);
  tscz = new TextField("1",1);
  tw = new TextField("20",1);
  th = new TextField("20",1);
  tl = new TextField("20",1);
  ti = new TextField("3",1);
  tccr = new TextField("128",1);
  tccg = new TextField("128",1);
  tccb = new TextField("128",1);
  tcca = new TextField("255",1);
  tlw = new TextField("3",1);
  
  UITop = new Panel();
  UITop.setLayout(new BorderLayout());
  UI = new Panel();
  UI1 = new Panel();
  
  UI.add(new Label("RX"));
  UI.add(trcx);
  UI.add(new Label("RY"));
  UI.add(trcy);
  UI.add(new Label("RZ"));
  UI.add(trcz);
  
  UI.add(new Label("TX"));
  UI.add(ttcx);
  UI.add(new Label("TY"));
  UI.add(ttcy);
  UI.add(new Label("TZ"));
  UI.add(ttcz);
  
  UI.add(new Label("SX"));
  UI.add(tscx);
  UI.add(new Label("SY"));
  UI.add(tscy);
  UI.add(new Label("SZ"));
  UI.add(tscz);
  
  UITop.add(UI, BorderLayout.NORTH);
  
  UI1.add(new Label("w"));
  UI1.add(tw);
  UI1.add(new Label("h"));
  UI1.add(th);
  UI1.add(new Label("l"));
  UI1.add(tl);
  UI1.add(new Label("i"));
  UI1.add(ti);
  
  UI1.add(new Label("RF"));
  UI1.add(tccr);
  UI1.add(new Label("GF"));
  UI1.add(tccg);
  UI1.add(new Label("BF"));
  UI1.add(tccb);
  UI1.add(new Label("AF"));
  UI1.add(tcca);
  
  UI1.add(new Label("LW"));
  UI1.add(tlw);
  
  UITop.add(UI1, BorderLayout.CENTER);
  
  
  
  this.add(UITop, BorderLayout.NORTH);
  
  stroke(0);            // Set line drawing color to white
  
  ax = 0;
  ay = 0;
  az = 0;
  cx = 0;
  cy = 0;
  cz = 1;
}
void draw() {
  // setup view
  background(255,255,255);
  lw = float(tlw.getText());
  strokeWeight(lw);
  translate(cx,cy,cz);
  //scale(cz,cz,cz);
  rotateX(radians(ax));
  rotateY(radians(ay));
  rotateZ(radians(az));
  
  // get rotation
  rcx = float(trcx.getText());
  rcy = float(trcy.getText());
  rcz = float(trcz.getText());
  // get translation
  tcx = float(ttcx.getText());
  tcy = float(ttcy.getText());
  tcz = float(ttcz.getText());
  // get scale
  scx = float(tscx.getText());
  scy = float(tscy.getText());
  scz = float(tscz.getText());
  // get w,h,l, iter
  w = float(tw.getText());
  h = float(th.getText());
  l = float(tl.getText());
  niter = int(ti.getText());
  // get fill
  ccr = int(tccr.getText());
  ccg = int(tccg.getText());
  ccb = int(tccb.getText());
  cca = int(tcca.getText());
  fill(ccr,ccg,ccb,cca);
  translate(width/2,height/2,0);
  translate(pcx,pcy,pcz);
  for(int i=0;i<niter;i++) {
    box(w,h,l);
    translate(tcx,tcy,tcz);
    rotateX(radians(rcx)); 
    rotateY(radians(rcy));
    rotateZ(radians(rcz));
    scale(scx,scy,scz);
  }
}
void mousePressed() {
  ex = mouseX;
  ey = mouseY;
  if (mouseButton == LEFT) {

  } 
  else if (mouseButton == RIGHT) {

  } 
  else {

  }
}
void mouseDragged() 
{
  if (mouseButton == LEFT) {
    noLoop();
    az += (ex-mouseX)*0.25;
    ax += (ey-mouseY)*0.25;

    ex = mouseX;
    ey = mouseY;
    loop();
  } 
  else if (mouseButton == RIGHT) {
    cz += (ex-mouseX);
    cz += (ey-mouseY);
    ex = mouseX;
    ey = mouseY;
    loop();
  } 
  else {
    noLoop();
    cx -= ex-mouseX;
    cy -= ey-mouseY;
    ex = mouseX;
    ey = mouseY;
    loop();
  }
}
void keyPressed() {
  if (key == CODED) {
    if (keyCode == UP) {
      cy += 1;
    } 
    else if (keyCode == DOWN) {
      cy -= 1;
    } 
    else if (keyCode == LEFT) {
      cx += 1;
    } 
    else if (keyCode == RIGHT) {
      cx -= 1;
    }
  }
}


